/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author michael
 *
 */
public class ProductsData {
	
	private static final String TAG = "ProductsData";
	private Context mContext;
	private DataManager mDataManager;
	private SQLiteDatabase mDb;
		
	public ProductsData(Context context) {
		Log.d(TAG, "ProductsData ctor called.");
		mContext = context;
	}
	
	public ProductsData open() throws SQLException {
		Log.d(TAG, "open called on ProductsData. Creating a new DatabaseHelper and getting a writable database.");
		mDataManager = new DataManager(mContext);
		mDb = mDataManager.getWritableDatabase();
		Log.d(TAG, "Returning this instance.");
		return this;
	}
	
	public void close() {
		Log.d(TAG, "close called on ProductsData. Closing the DatabaseHelper.");
		mDataManager.close();
	}
	
	public Cursor getAllProducts() {
		Log.d(TAG, "Getting all products.");
		String sql = "select _id, (select name from categories where _id = categoryId) as category, name, description, price, barcode"
				+ " from " + DataManager.Tables.Products.TABLE_NAME;
		Cursor cursor = mDb.rawQuery(sql, null);
		if(cursor != null)
			cursor.moveToFirst();
		Log.d(TAG, "Returning cursor.");
		return cursor;
	}
	
	public Cursor getProductById(Long productId) {
		Log.d(TAG, "Getting product by Id.");
		String sql = "select _id, (select name from categories where _id = categoryId) as category, name, description, price, barcode"
				+ " from " + DataManager.Tables.Products.TABLE_NAME + " where _id = ?";
		Cursor cursor = mDb.rawQuery(sql, new String[] { String.valueOf(productId)});
		if(cursor != null)
			cursor.moveToFirst();
		Log.d(TAG, "Returning cursor.");
		return cursor;
	}
	
	public long insertProduct(Long categoryId, String name, String description, double price, String barcode) {
		Log.d(TAG, "insertProduct called.");
		ContentValues values = new ContentValues();
		values.put(DataManager.Tables.Products.COLUMN_CATEGORY_ID, categoryId);
		values.put(DataManager.Tables.Products.COLUMN_NAME, name);
		values.put(DataManager.Tables.Products.COLUMN_DESCRIPTION, description);
		values.put(DataManager.Tables.Products.COLUMN_PRICE, price);
		values.put(DataManager.Tables.Products.COLUMN_BARCODE, barcode);
		Log.d(TAG, "Executing query.");
		return mDb.insert(DataManager.Tables.Products.TABLE_NAME, null, values);
	}
	
	public boolean updateProduct(Long productId, Long categoryId, String name, String description, double price, String barcode) {
		Log.d(TAG, "updateProduct called.");
		ContentValues values = new ContentValues();
		values.put(DataManager.Tables.Products.COLUMN_CATEGORY_ID, categoryId);
		values.put(DataManager.Tables.Products.COLUMN_NAME, name);
		values.put(DataManager.Tables.Products.COLUMN_DESCRIPTION, description);
		values.put(DataManager.Tables.Products.COLUMN_PRICE, price);
		values.put(DataManager.Tables.Products.COLUMN_BARCODE, barcode);
		Log.d(TAG, "Executing query.");
		return mDb.update(DataManager.Tables.Products.TABLE_NAME, 
				values, 
				DataManager.Tables.Products.COLUMN_ID + "=?", 
				new String[] { String.valueOf(productId)}) != 0;
	}
	
	public boolean deleteProduct(Long productId){
		Log.d(TAG, "Delete product called for product " + String.valueOf(productId));
		int rows= mDb.delete(DataManager.Tables.Products.TABLE_NAME, 
				DataManager.Tables.Products.COLUMN_ID + "=?",
				new String[] { String.valueOf(productId) });
		if(rows != 0){
			rows = mDb.delete(DataManager.Tables.ProductsByList.TABLE_NAME, 
					DataManager.Tables.ProductsByList.COLUMN_PRODUCT_ID + "=?", 
					new String[] { String.valueOf(productId) });
		}
		return rows != 0;
	}
}
